﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphicsFramework.Drawing.Primitive
{
    public class TextBlock : BasePrimitive
    {
        private string text;

        public TextBlock(Enum.EnumDrawingType type, string description, Enum.EnumMouseClicks click)
            : base(type, description, click)
        {
            Text = "Text";
            Initialize(0, 0, 0, 0);

            properties = Property.DefaultProperty.Text.Clone();
        }

        public TextBlock()
            : base(Enum.EnumDrawingType.POLYGON, "Draw Text", Enum.EnumMouseClicks.ONE)
        {
            Text = "Text";
            Initialize(0, 0, 0, 0);

            properties = Property.DefaultProperty.Text.Clone();
        }

        public TextBlock(string text, Entity.Point2F point)
            : base(Enum.EnumDrawingType.POLYGON, "Draw Text", Enum.EnumMouseClicks.ONE)
        {
            Text = text;
            Initialize(point.X, point.Y, point.X + 1, point.Y - 1);

            properties = Property.DefaultProperty.Text.Clone();
        }

        private void Initialize(float x1, float y1, float x2, float y2)
        {
            properties = Property.DefaultProperty.Text.Clone();

            points = new List<Entity.Point2F>();

            points.Add(new Entity.Point2F(x1, y1));
            points.Add(new Entity.Point2F(x1, y2));
            points.Add(new Entity.Point2F(x2, y2));
            points.Add(new Entity.Point2F(x2, y1));
           
            //Update the drawing after adding points.
            UpdateDrawing();
            UpdateCenter();

        }

        public override GraphicBase Clone()
        {
            TextBlock graphic = new TextBlock(text, points[0].Clone());

            //Clone base properties
            base.Clone(graphic);

            return graphic;
        }

        public override void OnRender(GraphicsFramework graphics)
        {
            if (textproperties.Updated)
            {
                Entity.Point2I size = graphics.MeasureString(textproperties, text);
                Initialize(points[0].X, points[0].Y, points[0].X + graphics.ToPointWidth(size.X), points[0].Y - graphics.ToPointHeight(size.Y));
            }

            graphics.DrawText(properties, textproperties, points[0], text);
        }

        public override void UpdateGuide(List<Entity.Point2F> mousedown, Entity.Point2F mousemove)
        {
            Initialize(mousedown[0].X, mousedown[0].Y, mousedown[0].X + 1, mousedown[0].Y - 1);
        }

        public string Text
        {
            get { return text; }
            set { text = value; }
        }
    }
}
